<!--商品代买-->
<style>

</style>

<template>
<div style="width: 50%;margin: 0 auto">
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户姓名" prop="username" style="width: 700px">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="用户地址" prop="address">
        <el-input v-model="ruleForm.address" style="width: 600px"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone" style="width: 600px;"></el-input>
      </el-form-item>
      <el-form-item label="购买信息" prop="purchaseInfo">
        <el-input v-model="ruleForm.purchaseInfo" style="width: 600px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">下单</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username:'',
        address:'',
        phone:'',
        purchaseInfo:''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
          { min: 3, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 3, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur' }
        ],
        purchaseInfo: [
          { required: true, message: '请输入购买信息', trigger: 'blur' },
          { min: 3, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url="http://localhost:8080/commoditys/purchase";
          let formData=this.qs.stringify(this.ruleForm)
          this.axios.post(url,formData).then((response) => {
            let responseBody=response.data;
            if (responseBody.state==20000){
              this.$message({
                message: '恭喜你，这是一条成功消息',
                type: 'success'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

